package com.example.user.myzzyl_android;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;


public class ZhuYeFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match

    int REQUEST_CODE = 1;
    ListView listView;
    ZhuYeMianAdpter zhuYeMianAdpter;
    List<String> list;
    JsonText2 jsonText2;
    List<String> data = new ArrayList<>();
    List<String> title = new ArrayList<>();
    List<String> id = new ArrayList<>();
    ImageButton imageButton;
    ImageButton show_QR;
   // MarqueeView marqueeView;
    // TODO: Rename and change types of parameters
    public static final String TAG = "ZhuYeFragment";
    public ZhuYeFragment() {
        // Required empty public constructor
    }

    public static ZhuYeFragment newInstance() {
        ZhuYeFragment fragment = new ZhuYeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_zhu_ye_mian, container, false);
        ButterKnife.bind(this,view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
      //  marqueeView = (MarqueeView) LayoutInflater.from(getContext()).inflate(R.layout.item2, null).findViewById(R.id.MarqueeView);
        listView = view.findViewById(R.id.list_item);
        imageButton = view.findViewById(R.id.btn_showSideMenu);
        show_QR = view.findViewById(R.id.show_QR);
        list = new ArrayList<>();
        list.add("item1");
        list.add("item2");
        list.add("item3");
        list.add("item4");
        zhuYeMianAdpter = new ZhuYeMianAdpter(getContext(),list,data,title,id);
        listView.setAdapter(zhuYeMianAdpter);
        OkGo.<String>post("http://zhihui.expo2017.net.cn/zzwhe/resources/ybzx/hotList")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.i("nnnnn",response.body());
                         jsonText2 = JSON.parseObject(response.body(),JsonText2.class);
                        for (int i = 0; i < jsonText2.getData().getContent().size(); i++) {
                            title.add(jsonText2.getData().getContent().get(i).getMc());
                            id.add(jsonText2.getData().getContent().get(i).getId());
                        }
                        zhuYeMianAdpter.notifyDataSetChanged();
                        zhuYeMianAdpter.marqueeView.startFlipping();
                    }
                });



        OkGo.<String>post("http://zhihui.expo2017.net.cn/zzwhe/resources/syLb/list")
                .tag(this)
                .params("page",1)
                .params("size",3)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.i("tian",response.body());
                        JsonText jsonText =  JSON.parseObject(response.body(),JsonText.class); //json转换成类
                        Log.i("jjjjj",jsonText.getData().getContent().get(0).getTp());
                        for (int a=0;a<jsonText.getData().getContent().size();a++) {
                            data.add("http://zhihui.expo2017.net.cn/zzwhe/"+jsonText.getData().getContent().get(a).getTp());
                            Log.i("imgssss",data.get(a));
                        }
                        zhuYeMianAdpter.notifyDataSetChanged();
                    }

                });
        show_QR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               startActivity(new Intent(getContext(),UserActivity.class));
//                FragmentManager fm = getActivity().getFragmentManager();
//                UserActivity fragment = new UserActivity();
//                fm.beginTransaction().replace(R.id.FrameLayout,fragment).commit();
            }
        });


    }
    @Override
    public void onStop() {
        super.onStop();
//        marqueeView.stopFlipping();
        zhuYeMianAdpter.marqueeView.stopFlipping();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("data",String.valueOf(data));
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(getContext(), "解析结果:" + result, Toast.LENGTH_LONG).show();
                    Log.e(TAG,"解析结果:" + result);
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(getContext(), "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
